package com.drugStorage.spring.ServiceImpl;

import java.util.Date;
import java.util.List;

import com.drugStorage.domain.DetailDrugStorageList;
import com.drugStorage.domain.DrugScience;
import com.drugStorage.domain.InDrugList;
import com.drugStorage.domain.InStorageBill;
import com.drugStorage.domain.SumDrugStorageList;
import com.drugStorage.hibernate.dao.DetailDrugStorageListDao;
import com.drugStorage.hibernate.dao.InStorageBillDao;
import com.drugStorage.hibernate.dao.SumDrugStorageListDao;
import com.drugStorage.spring.service.InStorageService;

public class InStorageServiceImpl implements InStorageService {

	private SumDrugStorageListDao sumDrugStorageListDao;

	private DetailDrugStorageListDao detailDrugStorageListDao;

	private InStorageBillDao inStorageBillDao;

	public SumDrugStorageListDao getSumDrugStorageListDao() {
		return sumDrugStorageListDao;
	}

	public void setSumDrugStorageListDao(
			SumDrugStorageListDao sumDrugStorageListDao) {
		this.sumDrugStorageListDao = sumDrugStorageListDao;
	}

	public DetailDrugStorageListDao getDetailDrugStorageListDao() {
		return detailDrugStorageListDao;
	}

	public void setDetailDrugStorageListDao(
			DetailDrugStorageListDao detailDrugStorageListDao) {
		this.detailDrugStorageListDao = detailDrugStorageListDao;
	}

	public InStorageBillDao getInStorageBillDao() {
		return inStorageBillDao;
	}

	public void setInStorageBillDao(InStorageBillDao inStorageBillDao) {
		this.inStorageBillDao = inStorageBillDao;
	}

	public void AddInDrugListToInStorageBill(InDrugList inDrugList, int id) {
		InStorageBill inStorageBill = inStorageBillDao.getInStorageBill(id);
		inDrugList.setInStorageBill(inStorageBill);
		// 获得入库单的信息
	DrugScience drug = inDrugList.getDrug();
		int DrugID = inDrugList.getDrug().getId();

		int amount = inDrugList.getAmount();
		Date deadDate = inDrugList.getDeadDate();
		Date proDate = inDrugList.getProDate();
		float inPrice = inDrugList.getInPrice();
		float outPrice = inDrugList.getOutPrice();
		SumDrugStorageList sumDrugStorageList = new SumDrugStorageList();
		sumDrugStorageList.setDrug(drug);
		sumDrugStorageList.setAmount(amount);
		// 更新简要库存数量
		if(sumDrugStorageListDao.getSumDrugStorageListByDrugID(DrugID)==null)
		{
			sumDrugStorageListDao.saveSumDrugStorageList(sumDrugStorageList);
		}
		else{ sumDrugStorageList = sumDrugStorageListDao
				.getSumDrugStorageListByDrugID(DrugID);
		sumDrugStorageList.setAmount(sumDrugStorageList.getAmount() + amount);
		sumDrugStorageListDao.updateSumDrugStorageList(sumDrugStorageList);
		}
		// 更新详细库存信息
		DetailDrugStorageList detailDrugStorageList = new DetailDrugStorageList();
		detailDrugStorageList.setDrug(drug);
		detailDrugStorageList.setAmount(amount);
		detailDrugStorageList.setDeadDate(deadDate);
		detailDrugStorageList.setProDate(proDate);
		detailDrugStorageList.setInPrice(inPrice);
		detailDrugStorageList.setOutPrice(outPrice);

		
		detailDrugStorageListDao
				.SaveDetailDrugStorageList(detailDrugStorageList);
		//System.out.println(sumDrugStorageList);
		//System.out.println(inDrugList);

		inStorageBillDao.SaveInDrugListById(inDrugList);

	}

	public void DelInDrugList(InDrugList inDrugList) {

	}

	public List<InDrugList> getInDrugListByInstorageBillId(int id) {
		List<InDrugList> IDlist = inStorageBillDao
				.getInDrugListByInStorageBillid(id);

		return IDlist;
	}

	public InStorageBill getInStorageBill(int id) {
		InStorageBill inStorageBill = inStorageBillDao.getInStorageBill(id);
		return inStorageBill;
	}

	public List<InStorageBill> getInStorageBillList() {

		return inStorageBillDao.getInStorageBillList();
	}

	public void saveInStorageBill(InStorageBill instorageBill) {
		inStorageBillDao.saveInStorageBill(instorageBill);

	}

}
